package testng;

import static org.testng.AssertJUnit.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import javax.annotation.PreDestroy;

import org.testng.ITest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import train.Schedule;
import train.Train;

public class TrainTest implements ITest {

	Train train;

	@Test
	public void depend() {

	}

	@Test(expectedExceptions = ArrayIndexOutOfBoundsException.class, dependsOnMethods = "depend", groups = "broken")
	public void canBookMoreThenAvailable() {
		train.bookAll();
		train.bookTicket();
	}

	@Test(expectedExceptions = ArrayStoreException.class)
	public void canBookTciketWhenTrackCanceld() {
		train.cancelTrac();
		train.bookTicket();
	}

	@BeforeMethod(alwaysRun = true)
	public void beforeTest() {
		this.train = new Train();
	}

	@Test
	public void areDestinationsSupportedBadDesign() {
		assertEquals("Are not Equal", "12:00",
				train.getTrackSchedule(Schedule.VARNA.getDestination()));
		assertEquals("Are not Equal", "16:00",
				train.getTrackSchedule(Schedule.BLAGOEVGRAD.getDestination()));
		assertEquals("Are not Equal", "4:15",
				train.getTrackSchedule(Schedule.SOFIA.getDestination()));
		assertEquals("Are not Equal", "14:30",
				train.getTrackSchedule(Schedule.MEZDRA.getDestination()));
	}

	@Test(groups = "Good")
	public void areDestinationsSupportedGoodDesign() {
		Properties p = new Properties();

		try {
			p.load(new FileInputStream(new File("res/schedule.properties")));
			for (Enumeration<?> e = p.propertyNames(); e.hasMoreElements();) {
				String destination = (String) e.nextElement();
				String time = p.getProperty(destination);

				assertEquals("Are not Equal", time, train.getTrackSchedule(destination));
			}
		} catch (FileNotFoundException e1) {
			fail("Cant locate schedule.properties");
		} catch (IOException e2) {

		}
	}
	
	@Parameters("schedule_properties")
	@Test(groups = {"Best", "Good"})
	public void areDestinationsSupportedBestDesign(String schedulPropsFile) {
		Properties p = new Properties();

		try {
			p.load(new FileInputStream(new File(schedulPropsFile)));
			for (Enumeration<?> e = p.propertyNames(); e.hasMoreElements();) {
				String destination = (String) e.nextElement();
				String time = p.getProperty(destination);

				assertEquals("Are not Equal", time, train.getTrackSchedule(destination));
			}
		} catch (FileNotFoundException e1) {
			fail("Cant locate schedule.properties");
		} catch (IOException e2) {

		}
	}

	@Override
	public String getTestName() {
		return "[Train Test]";
	}
}
